package net.alche.al.util;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.swing.text.html.HTML.Tag;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.BeanCopier;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import net.alche.al.util.far.pojo.PageList;
import net.alche.al.util.far.pojo.VueProxy;
import net.alche.fa.core.FaClass;
import net.alche.fa.core.FaException;
import net.alche.fa.core.SageStone;
import net.alche.fa.core.doing.DotDo;
import net.alche.fa.core.model.FaValue;

import java.util.Date;
import java.util.HashMap;
import java.util.Arrays;
import java.util.Calendar;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.time.LocalDate;
import java.time.LocalDateTime;

// M
@SuppressWarnings("all")
public class M {

  public M() {
  }

  public static boolean in(Integer... parms) {
    Integer val = null;
    for (Integer v : parms) {
      if (val == null) {
        if (v == null) {
          return false;
        }
        val = v;
      } else if (val.intValue() == v.intValue()) {
        return true;
      }
    }
    return false;
  }

  public static boolean in(String... parms) {
    String val = null;
    for (String v : parms) {
      if (val == null) {
        if (v == null) {
          return false;
        }
        val = v;
      } else if (val.equals(v)) {
        return true;
      }
    }
    return false;
  }

  public static boolean contain(String[] ls, String key) {
    return ArrayUtil.contains(ls, key);
  }

  public static boolean contain(List ls, Object key) {
    if (ls != null && key != null) {
      for (Object obj : ls) {
        if (obj.equals(key)) {
          return true;
        }
      }
    }
    return false;
  }

  public static boolean eq(Integer val, Integer v2) {
    if (val != null && v2 != null) {
      return val.intValue() == v2.intValue();
    } else
      return val == null && v2 == null;
  }

  public static boolean neq(Integer val, Integer v2) {
    return !eq(val, v2);
  }

  public static boolean eq(Object val, Object v2) {
    if (val == null && v2 == null) {
      return true;
    }
    if (val == null) {
      return false;
    }
    if (val.equals(v2)) {
      return true;
    }
    return false;
  }

  public static <T> T i(List list, Integer i) {
    if (list == null || i == null || list.size() < i + 1 || i < 0) {
      return null;
    }
    return (T) list.get(i);
  }

  public static void add(List list, Integer index, Object item) {
    if (list == null)
      return;
    list.add(index, item);
  }

  public static void delLast(List list) {
    if (list == null || list.isEmpty())
      return;
    list.remove(list.size() - 1);
  }

  public static <T> T first(List list) {
    if (list == null || list.isEmpty())
      return null;
    return (T) list.get(0);
  }

  public static <T> T last(List list) {
    if (list == null || list.isEmpty())
      return null;
    return (T) list.get(list.size() - 1);
  }

  public static boolean noEmp(Object obj) {
    return !emp(obj);
  }

  public static boolean notEmpty(Object obj) {
    return !emp(obj);
  }

  public static boolean emp(Object obj) {
    if (obj == null) {
      return true;
    }
    if (obj instanceof List && ((List) obj).isEmpty()) {
      return true;
    }
    if (obj instanceof Map && ((Map) obj).isEmpty()) {
      return true;
    }
    if (obj instanceof String && ((String) obj).equals("")) {
      return true;
    }
    return false;
  }

  public static Object readField(Object obj, String colName) {
    if (obj == null) {
      return null;
    }
    Object obb = null;
    try {
      Field fi = obj.getClass().getField(colName);
      obb = fi.get(obj);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    return obb;
  }

  public static Object invoker(Object obj, String methodName, Object... params) {
    if (obj == null) {
      return null;
    }
    // 参数类型集合
    Class[] classes = new Class[] {};
    FaValue ov = new FaValue();
    if (params != null) {
      classes = new Class[params.length];
      for (int i = 0; i < params.length; i++) {
        Object item = params[i];
        if (item == null) {
          classes[i] = null;
        } else {
          classes[i] = item.getClass();
        }
      }
    }
    Object backObj = null;
    Method method = null;
    try {
      method = obj.getClass().getMethod(methodName, classes);
      method.setAccessible(true);
      // 可变参数
      Object dynObj = DotDo.isDynamicParam(method, params);
      if (dynObj != null) {
        backObj = method.invoke(obj, dynObj);
      } else {
        backObj = method.invoke(obj, params);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return backObj;
  }

  public static boolean emp(Object obj, String... keys) {
    Object obb = obj;
    for (int i = 0; i < keys.length; i++) {
      String key = keys[i];
      obb = readField(obb, key);
      if (obb == null) {
        return true;
      }
      if (i + 1 >= keys.length) {
        return emp(obb);
      }
    }
    return false;
  }

  public static Object objByC(Object obj, String... keys) {
    Object obb = obj;
    for (int i = 0; i < keys.length; i++) {
      String key = keys[i];
      obb = readField(obb, key);
      if (obb == null) {
        return null;
      }
    }
    return obb;
  }

  public static List<String> keys(Object obj) {
    List list = new ArrayList<>();
    if (obj != null) {
      if (obj instanceof Map vv) {
        for (Object key : vv.keySet()) {
          list.add(key);
        }
      } else {
        Field[] fis = obj.getClass().getFields();
        for (Field fd : fis) {
          list.add(fd.getName());
        }
      }
    }
    return list;
  }

  public static boolean noEmp(Object obj, String... keys) {
    return !emp(obj, keys);
  }

  public static String dot2(Double val) {
    if ((val == null)) {
      return "0.00";
    }
    return String.format("%.2f", val);
  }

  public static String replaceAll(String str, String str1, String str2) {
    if (str == null) {
      return "";
    }
    return str.replaceAll(str1, str2);
  }

  public static boolean neq(Object val, Object v2) {
    return !M.eq(val, v2);
  }

  public static Map getMap(Map map, String key) {
    if (map == null) {
      return null;
    }
    Object val = map.get(key);
    if (val != null) {
      return (Map) val;
    }
    return null;
  }

  public static List ls(Map map, String key) {
    if (map == null) {
      return null;
    }
    Object val = map.get(key);
    if (val != null) {
      return (List) val;
    }
    return null;
  }

  public static void remove(Map mp, String key) {
    if (mp == null) {
      return;
    }
    mp.remove(key);
  }

  public static String firstLowerCase(String str) {
    if (str != null && str.length() > 0) {
      return str.substring(0, 1).toLowerCase() + str.substring(1);
    }
    return null;
  }

  public static String firstUpperCase(String str) {
    if (str != null && str.length() > 0) {
      return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
    return null;
  }

  public static String humpToUnder(String name) {
    name = firstLowerCase(name);
    return name.replaceAll("([A-Z])", "_$1").toLowerCase();
  }

  public static String underToHump(String name) {
    if (name == null)
      return "";
    String[] sp = name.split("_");
    String txt = "";
    for (int i = 0; i < sp.length; i++) {
      if (i == 0) {
        txt += sp[i];
      } else {
        txt += firstUpperCase(sp[i]);
      }
    }
    return txt;
  }

  public static String hyphenToClass(String name) {
    if (name == null)
      return "";
    String[] sp = name.split("-");
    String txt = "";
    for (int i = 0; i < sp.length; i++) {
      txt += firstUpperCase(sp[i]);
    }
    return txt;
  }

  public static Integer i(Map map, String key) {
    return M.i(map, key, 0);
  }

  public static Double d(Map map, String key) {
    return M.dor(map, key, 0d);
  }

  public static Double dor(Map map, String key, Double deft) {
    if (map == null) {
      return deft;
    }
    Object val = map.get(key);
    if (val != null) {
      if (val instanceof String) {
        return Double.parseDouble((String) val);
      }
      if (val instanceof Long) {
        return ((Long) val).doubleValue();
      }
      if (val instanceof Integer) {
        return ((Integer) val).doubleValue();
      }
      return (Double) val;
    }
    return deft;
  }

  public static Integer ior(Map map, String key) {
    return M.i(map, key, null);
  }

  public static Integer ior(Map map, String key, Integer deft) {
    return M.i(map, key, deft);
  }

  public static Integer i(Map map, String key, Integer deft) {
    if (map == null) {
      return deft;
    }
    Object val = map.get(key);
    if (val != null) {
      if (val instanceof String) {
        return Integer.parseInt((String) val);
      }
      if (val instanceof Long) {
        return ((Long) val).intValue();
      }
      if (val instanceof Double) {
        return ((Double) val).intValue();
      }
      return (Integer) val;
    }
    return deft;
  }

  public static Integer getInt(Map map, String key) {
    if (map == null) {
      return 0;
    }
    Object val = map.get(key);
    if (val != null) {
      if (val instanceof String) {
        return Integer.parseInt(val.toString());
      }
      return (Integer) val;
    }
    return 0;
  }

  public static <T> T get(Map map, String key) {
    if (map != null) {
      return (T) map.get(key);
    }
    return null;
  }

  public static <T> T get(Map map, Integer key) {
    if (map != null) {
      return (T) map.get(key);
    }
    return null;
  }

  public static <T> T get(Object obj, String key) {
    return (T) Far.java.getAttr(obj, key);
  }

  public static FaValue get(SageStone obj, String key) {
    return obj.getVar(key);
  }

  public static Boolean getBool(Map map, String key) {
    if (map == null) {
      return false;
    }
    Object val = map.get(key);
    if (val != null) {
      if (val instanceof String) {
        return Boolean.parseBoolean(val.toString());
      }
      return (Boolean) val;
    }
    return false;
  }

  public static Boolean b(Map map, String key) {
    return M.getBool(map, key);
  }

  public static Boolean has(Map map, String key) {
    if (map != null && map.containsKey(key))
      return true;
    return false;
  }

  public static String s(Map map, String key) {
    return M.getStr(map, key);
  }

  public static String s(Map map, String key, String deft) {
    return M.getStr(map, key, deft);
  }

  public static String getStr(Map map, String key) {
    if (map == null) {
      return "";
    }
    Object val = map.get(key);
    if ((val != null)) {
      if (val instanceof Double) {
        Double db = (Double) val;
        if (db.intValue() == db.doubleValue()) {
          return "" + db.intValue();
        }
      }
      return Far.tl.parseStr(val);
    }
    return "";
  }

  public static String getStr(Map map, String key, String deft) {
    String str = M.getStr(map, key);
    if (deft != null && emp(str)) {
      return deft;
    }
    return str;
  }

  public static void put(Map map, String key, Object val) {
    if (map != null) {
      map.put(key, val);
    }
  }

  public static void put(Map map, Integer key, Object val) {
    if (map != null) {
      map.put(key, val);
    }
  }

  public static String str(Object val) {
    if (val != null) {
      if (val instanceof Double) {
        Double db = (Double) val;
        if (db.intValue() == db.doubleValue()) {
          return "" + db.intValue();
        }
        if (val instanceof Date) {
          return AC.date.fmt((Date) val, "yyyy-MM-dd HH:mm:ss");
        }
        return Far.tl.parseStr(val);
      }
    }
    return "";
  }

  public static Integer intt(Object val) {
    if (val != null) {
      try {
        return Integer.parseInt(M.str(val));
      } catch (Exception ex) {
        return 0;
      }
    }
    return 0;
  }

  public static Double dob(Object val) {
    if (val != null) {
      try {
        return Double.parseDouble(M.str(val));
      } catch (Exception ex) {
        return 0.0;
      }
    }
    return 0.0;
  }

  public static List<String> split(String str, String ch) {
    if (noEmp(str)) {
      return Arrays.asList(str.split(ch));
    }
    return new ArrayList<>();
  }

  public static List<Integer> splitToInt(String str, String ch) {
    if (noEmp(str)) {
      ArrayList ls = new ArrayList<>();
      String[] arr = str.split(ch);
      for (String item : arr) {
        if (M.noEmp(item))
          ls.add(Integer.parseInt(item));
      }
      return ls;
    }
    return new ArrayList<>();
  }

  public static List<Integer> toInts(String str) {
    return splitToInt(str, ",");
  }

  public static List defLs(Object... objs) {
    ArrayList ls = new ArrayList<>();
    for (Object obj : objs) {
      ls.add(obj);
    }
    return ls;
  }

  public static void push(List list, Object item) {
    if (list != null) {
      list.add(item);
    }
  }

  public static void push(VueProxy vp, Object item) {
    if (vp != null && vp.v != null) {
      ((List) vp.v).add(item);
      vp.renderOnce();
    }
  }

  public static void clear(List list) {
    if (list != null) {
      list.clear();
    }
  }

  public static void clear(Map mp) {
    if (mp != null) {
      mp.clear();
    }
  }

  public static void clear(VueProxy vp) {
    if ((vp != null && vp.v != null)) {
      ((List) vp.v).clear();
      vp.renderOnce();
    }
  }

  public static void del(List list, Integer index) {
    if ((list != null && index != null && list.size() > index)) {
      list.remove(index.intValue());
    }
  }

  public static void delItem(List<Integer> list, Integer index) {
    if ((list != null && index != null && !list.isEmpty())) {
      list.remove(index);
    }
  }

  public static void delItem(List<String> list, String index) {
    if ((list != null && index != null && !list.isEmpty())) {
      list.remove(index);
    }
  }

  public static void del(Map mp, String key) {
    if (mp != null) {
      mp.remove(key);
    }
  }

  public static void del(VueProxy vp, Integer index) {
    if ((vp != null && vp.v != null && index != null)) {
      List list = (List) vp.v;
      if ((list.size() > index)) {
        list.remove(index.intValue());
        vp.renderOnce();
      }
    }
  }

  public static int len(VueProxy vp) {
    if (vp == null || vp.v == null) {
      return 0;
    }
    return M.len((List) vp.v);
  }

  public static int len(String val) {
    if (val == null) {
      return 0;
    }
    return val.length();
  }

  public static int len(List list) {
    if (list == null) {
      return 0;
    }
    return list.size();
  }

  public static int len_c(Object obj, String... keys) {
    Object obb = objByC(obj, keys);
    if (obb == null) {
      return 0;
    }
    if (obb instanceof List vv) {
      return vv.size();
    }
    if (obb instanceof String vv) {
      return vv.length();
    }
    return 0;
  }

  public static Object transVal(Object val, String type) {
    if (val != null) {
      switch ((type)) {
        case "string": {
          if (!(val instanceof String)) {
            return M.str(val);
          }
          break;
        }
        case "int": {
          if (!(val instanceof Integer)) {
            return M.intt(val);
          }
          break;
        }
        case "double": {
          if (!(val instanceof Double)) {
            return M.dob(val);
          }
          break;
        }
      }
    }
    return val;
  }

  public static Object transVal(Object val, Class cls) {
    if (val != null) {
      if (cls.equals(String.class) && !(val instanceof String)) {
        return M.str(val);
      }
      if (cls.equals(Integer.class) && !(val instanceof Integer)) {
        return M.intt(val);
      }
      if (cls.equals(Double.class) && !(val instanceof Double)) {
        return M.dob(val);
      }
    }
    return val;
  }

  public static boolean x_in(Integer val, Integer... objs) {
    if (val == null) {
      return false;
    }
    if (objs == null || objs.length < 0) {
      return false;
    }
    for (int i = 0; i < objs.length; i++) {
      if (val.intValue() == objs[i].intValue()) {
        return true;
      }
    }
    return false;
  }

  public static boolean x_eq(Integer v1, Integer v2) {
    if (v1 == null && v2 == null) {
      return true;
    }
    if (v1 != null && v2 != null && v1.intValue() == v2.intValue()) {
      return true;
    }
    return false;
  }

  public static boolean x_neq(Integer v1, Integer v2) {
    return !M.x_eq(v1, v2);
  }

  public static double c0(Double val) {
    if (val == null) {
      return 0.0;
    }
    return val;
  }

  public static int c0(Integer val) {
    if (val == null) {
      return 0;
    }
    return val;
  }

  public static int year(Date val) {
    if (val == null)
      return 0;
    return DateUtil.year(val);
  }

  public static int month(Date val) {
    if (val == null)
      return 0;
    return DateUtil.month(val) + 1;
  }

  public static int day(Date val) {
    if (val == null)
      return 0;
    return DateUtil.dayOfMonth(val);
  }

  public static int hour(Date val) {
    if (val == null)
      return 0;
    return DateUtil.hour(val, true);
  }

  public static int minute(Date val) {
    if (val == null)
      return 0;
    return DateUtil.minute(val);
  }

  public static int sec(Date val) {
    if (val == null)
      return 0;
    return DateUtil.second(val);
  }

  public static int weekDay(Date val) {
    if (val == null)
      return 0;
    return DateUtil.dayOfWeek(val) - 1;
  }

  public static boolean startWith(String str, String eq) {
    if (str == null)
      return false;
    return str.startsWith(eq);
  }

  public static boolean endWith(String str, String eq) {
    if (str == null)
      return false;
    return str.endsWith(eq);
  }

  public static Double toDouble(String str) {
    if (str == null)
      return 0d;
    return Double.parseDouble(str);
  }

  public static Double toDouble(Integer i) {
    if (i == null)
      return 0d;
    return i.doubleValue();
  }

  public static Double db(Integer i) {
    if (i == null)
      return 0d;
    return i.doubleValue();
  }

  public static Double db(Double db) {
    return db;
  }

  public static Boolean toBool(String str) {
    if (str == null)
      return false;
    return Boolean.parseBoolean(str);
  }

  public static Integer toInt(String str) {
    if (str == null)
      return 0;
    return Integer.parseInt(str);
  }

  public static Integer toInt(Long lo) {
    if (lo == null)
      return 0;
    return lo.intValue();
  }

  public static String cut(String str, Integer cutIndex) {
    if (str != null) {
      return str.substring(cutIndex);
    }
    return str;
  }

  public static String toSpStrBet(List<String> ls, String space, Integer startIndex, Integer endIndex) {
    String txt = null;
    if (ls != null && !ls.isEmpty()) {
      int index = 0;
      for (String str : ls) {
        if (index < startIndex) {
          index++;
          continue;
        }
        if (endIndex > 0 && index > endIndex) {
          break;
        }
        if (txt == null) {
          txt = str;
        } else {
          txt += space + str;
        }
        index++;
      }
    }
    if (txt == null)
      txt = "";
    return txt;
  }

  public static String toSpStrIndex(List<String> ls, String space, Integer startIndex) {
    return toSpStrBet(ls, space, startIndex, -1);
  }

  public static String toSpStr(List<String> ls, String space) {
    return toSpStrBet(ls, space, -1, -1);
  }

  public static String toSpStr(String[] ls, String space) {
    String str = "";
    for (String item : ls) {
      str += str.equals("") ? item : space + item;
    }
    return str;
  }

  public static String json(Object obj) {
    return Far.tl.toJsonTxt(obj);
  }

  public static boolean noNull(Object obj) {
    if (obj != null) {
      return true;
    }
    return false;
  }

  public static boolean isNull(Object obj) {
    if (obj != null) {
      return false;
    }
    return true;
  }

  public static boolean noNull(Object obj, String... keys) {
    return objByC(obj, keys) != null;
  }

  public static boolean isNull(Object obj, String... keys) {
    return objByC(obj, keys) == null;
  }

  public static List link(List ls, List ls2) {
    if (ls == null) {
      ls = new ArrayList();
    }
    if (M.noEmp(ls2)) {
      ls.addAll(ls2);
    }
    return ls;
  }

  public static void pushLs(List ls, List ls2) {
    if (ls != null && M.noEmp(ls2)) {
      ls.addAll(ls2);
    }
  }

  // 设置任意对象的指定属性
  public static void setCol(Object obj, String col, Object val) {
  }

  public static String toStr(Object obj) {
    if (obj == null) {
      return "";
    }
    return obj.toString();
  }

  public static <T> T toJsonObj(String json, Class cls) {
    return Far.tl.fromJson(json, cls);
  }

  public static List toJsonObj_list(String json, Class cls) {
    return Far.tl.fromJsonList(json, cls);
  }

  public static FaValue toJsonObj(String json, FaClass cls) {
    return Far.tl.fromJson(json, cls);
  }

  public static FaValue toJsonObj_list(String json, FaClass cls) {
    return Far.tl.fromJsonList(json, cls);
  }

  public static <T> T toJsonObj(String json, Type type) {
    try {
      return (T) Far.gson.fromJson(json, type);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  public static boolean equal(String v1, String v2) {
    if (v1 != null) {
      return v1.equals(v2);
    }
    return v2 == null;
  }

  public static String spaceAdd(String txt, String space, String append) {
    if (AC.empty(txt)) {
      return append;
    } else {
      return txt + space + append;
    }
  }

  public static String fmt(Object... objs) {
    String txt = "";
    if (objs != null && objs.length > 0) {
      int i = 0;
      for (Object obj : objs) {
        if (i == 0) {
          txt = obj.toString();
        } else {
          txt = txt.replaceAll("x" + i, obj == null ? "null" : obj.toString());
        }
        i++;
      }
    }
    return txt;
  }

  public static int withCount(String str, String key) {
    int count = 0;
    if (M.emp(str)) {
      return 0;
    }
    int index = 0;
    while ((index = str.indexOf(key, index)) != -1) {
      index = index + key.length();
      count++;
    }
    return count;
  }

  public static <T> T anyVal(Object obj) {
    return (T) obj;
  }

  public static Object first(Map mp) {
    if (mp != null) {
      Set sets = mp.entrySet();
      for (Object obj : sets) {
        Entry entry = (Entry) obj;
        return entry.getValue();
      }
    }
    return null;
  }

  public static String subSuf(String str, String sp) {
    if (str == null)
      return "";
    int i = str.indexOf(sp);
    if (i >= 0)
      return str.substring(i + sp.length());
    return "";
  }

  public static String subI(String str, int index) {
    if (str == null)
      return "";
    return str.substring(index);
  }

  public static String toUrlParam(Map<String, Object> mp) {
    String str = "";
    if (mp != null && !mp.isEmpty()) {
      for (Entry<String, Object> en : mp.entrySet()) {
        if (en.getValue() == null)
          continue;
        if (!str.equals("")) {
          str += "&";
        }
        str += en.getKey() + "=" + en.getValue().toString();
      }
    }
    return URLEncodeUtil.encode(str);
  }

  public static FaValue copyCols(SageStone source, String[] cols) {
    return Far.java.copyCols(source, cols);
  }

  public static <T> T copyCols(Object source, String[] cols) {
    if (source == null)
      return null;
    Object target = ReflectUtil.newInstanceIfPossible(source.getClass());
    if (cols != null && cols.length > 0) {
      try {
        for (String col : cols) {
          Field fd = source.getClass().getDeclaredField(col);
          if (fd != null) {
            Object colVal = fd.get(source); // fd.get(col);
            if (colVal != null) {
              fd.set(target, colVal);
            }
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return (T) target;
  }

  public static void fillByStr(SageStone obj, String spChar, String[] cols) {
    Far.java.fillByStr(obj, spChar, cols);
  }

  public static void fillByStr(Object obj, String spChar, String[] cols) {
    if (obj == null)
      return;
    if (cols != null && cols.length > 0) {
      try {
        String[] sp = spChar.split("\\^\\|\\^");
        for (int i = 0; i < sp.length && i < cols.length; i++) {
          Field fd = obj.getClass().getDeclaredField(cols[i]);
          String str = sp[i];
          if (fd != null) {
            Object toVal = str;
            if (AC.notEmpty(str)) {
              if (fd.getType().equals(Integer.class)) {
                toVal = Integer.parseInt(str);
              } else if (fd.getType().equals(Double.class)) {
                toVal = Double.parseDouble(str);
              } else if (fd.getType().equals(Long.class)) {
                toVal = Long.parseLong(str);
              } else if (fd.getType().equals(Date.class)) {
                toVal = AC.date.parse(str, null);
              }
            }
            fd.set(obj, toVal);
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  public static boolean with(String str, String c) {
    return str != null && str.contains(c);
  }

  public static String replace(String str, String c, String to) {
    if (str != null) {
      return str.replaceFirst(c, to);
    }
    return str;
  }

  public static PageList pageBean(PageList pgList, List list) {
    return pgList;
  }

  public static <T> T get(HashMap map, String key) {
    if (map != null) {
      return (T) map.get(key);
    }
    return null;
  }

  public static Integer toInt(Double db) {
    if (db == null)
      return 0;
    return db.intValue();
  }

  public static String cutEnd(String str, int endLen) {
    return str.substring(0, str.length() - endLen);
  }

  public static void sleep(int sec) {
    try {
      Thread.sleep(1000 * sec);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static Map newMp(Object... objs) {
    Map map = new HashMap<>();
    for (int i = 0; i < objs.length; i++) {
      Object key = objs[i];
      Object val = objs[i + 1];
      map.put(key, val);
      i++;
    }
    return map;
  }

  public static List pickArrLs(List ls, String[] colName) {
    List newLs = new ArrayList<>();
    try {
      if (ls != null && !ls.isEmpty()) {
        for (Object obj : ls) {
          if (obj != null) {
            Object val = readField(obj, colName[0]);
            if (val != null) {
              newLs.add(val);
            }
          }
        }
      }
    } catch (Exception ex) {
      Far.logError(ex.getClass().getName(), ex.getMessage());
    }
    return newLs;
  }

  public static void delByCol(List ls, String col, Object eq) {
    if (ls != null && !ls.isEmpty()) {
      int index = 0;
      for (Object obj : ls) {
        Object val = readField(obj, col);
        if (val != null && val.equals(eq)) {
          ls.remove(index);
          break;
        }
        index++;
      }
    }
  }

  public static Object findByCol(List ls, String col, Object eq) {
    if (ls != null && !ls.isEmpty()) {
      int index = 0;
      for (Object obj : ls) {
        Object val = readField(obj, col);
        if (val != null && val.equals(eq)) {
          // ls.remove(index);
          // break;
          return obj;
        }
        index++;
      }
    }
    return null;
  }

  public static Date addDay(Date dt, int day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    cal.add(Calendar.DATE, day);
    return cal.getTime();
  }

  public static int intValue(Integer it) {
    return it.intValue();
  }

  public static String subPre(String str, String sp) {
    if (str == null)
      return "";
    int i = str.indexOf(sp);
    if (i >= 0)
      return str.substring(0, i);
    return "";
  }

  public static String subIendI(String str, int subI, int endI) {
    String ss = M.subI(str, subI);
    ss = M.cutEnd(ss, endI);
    return ss;
  }

  public static List<String> splitHalf(String str, String sp) {
    List<String> lls = new ArrayList<>();
    int i = str.indexOf(sp);
    if (i <= 0) {
      lls.add(str);
      lls.add("");
    } else {
      lls.add(str.substring(0, i));
      lls.add(str.substring(i + 1, str.length()));
    }
    return lls;
  }

  public static String trim(String str) {
    return str.trim();
  }

  public static String lowerCase(String str) {
    return str.toLowerCase();
  }

  public static Date addHour(Date dt, int hour) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    cal.add(Calendar.HOUR, hour);
    return cal.getTime();
  }

  public static String formatDateTime(Date dt) {
    return AC.date.fmtDatetime(dt);
  }

}